package com.mou.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mou.constant.MessageConstant;
import com.mou.context.BaseContext;
import com.mou.dto.ProductFavoritesDTO;
import com.mou.entity.Product;
import com.mou.entity.ProductFavorites;
import com.mou.exception.AccountErrorException;
import com.mou.exception.ProductFavoritesExistException;
import com.mou.mapper.ProductFavoritesMapper;
import com.mou.mapper.ProductMapper;
import com.mou.service.ProductFavoritesService;
import com.mou.vo.ProductFavoritesVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service("productFavoritesService")
public class ProductFavoritesServiceImpl extends ServiceImpl<ProductFavoritesMapper, ProductFavorites> implements ProductFavoritesService {

   @Autowired
   private ProductFavoritesMapper productFavoritesMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public void delete(ProductFavoritesDTO productFavoritesDTO) {
        productFavoritesMapper.deleteFavorites(productFavoritesDTO);
    }

    @Override
    public void add(ProductFavoritesDTO productFavoritesDTO) {
        Integer currentUserId = BaseContext.getCurrentId();
        if(currentUserId!=null){
            ProductFavorites pf = productFavoritesMapper.uniqueUserProductFavorites(productFavoritesDTO);
            if(pf==null){
                ProductFavorites productFavorites = new ProductFavorites();
                BeanUtils.copyProperties(productFavoritesDTO, productFavorites);
                productFavorites.setCreateTime(LocalDateTime.now());
                productFavoritesMapper.insert(productFavorites);
            }else{
                throw new ProductFavoritesExistException(MessageConstant.COLLECTION_EXIST);
            }
        }else{
            throw new AccountErrorException(MessageConstant.ACCOUNT_NOT_LOGIN);
        }

    }

    @Override
    public List<ProductFavoritesVO> getProductFavorites(ProductFavoritesDTO productFavoritesDTO) {
        List<ProductFavorites> productFavorites = productFavoritesMapper.getByUserId(productFavoritesDTO.getUserId());

        List<ProductFavoritesVO> productFavoritesVOList = new ArrayList<>();
        for (ProductFavorites pf : productFavorites) {
            ProductFavoritesVO productFavoritesVO = new ProductFavoritesVO();
            BeanUtils.copyProperties(pf,productFavoritesVO);
            Product product = productMapper.selectById(pf.getProductId());
            productFavoritesVO.setProductTitle(product.getTitle());
            productFavoritesVO.setProductImageUrl(product.getImageUrl());
            productFavoritesVO.setDescription(product.getDescription());
            productFavoritesVO.setPrice(product.getPrice());
            productFavoritesVOList.add(productFavoritesVO);
        }
        return productFavoritesVOList;
    }
}
